<template>
	<view>
		<input 
		class="uni-input" 
		type="text" 
		value="" 
		placeholder="请输入新密码" 
		v-model="newpassword" />
		<input 
		class="uni-input" 
		type="text" 
		value="" 
		placeholder="请输入确认密码" 
		v-model="renewpassword" />
		<view class="py-2 px-3">
			<button 
			class="bg-main text-white" 
			style="border-radius: 50rpx;border: 0;" 
			type="primary" 
			:disabled="disabled" 
			:class="{'bg-main-disabled':disabled}" 
			@click="submit()">设置</button>
		</view>
	</view>
</template>

<script>
	import {getUserSetPwd} from "@/network/login.js"
	export default {
		data() {
			return {
	
				newpassword:"",
				renewpassword:"",
			}
		},
		methods: {
			// 验证
			check(){
				if(this.newpassword!==this.renewpassword){
					uni.showToast({
						title: '两次密码不一致',
						icon: 'none'
					});
					return false
				}
				return true
			},
			submit(){
				if(!this.check()){
					return;
				}
				let data = {
					"pwd1":this.newpassword,
					"pwd2":this.renewpassword
				}
				getUserSetPwd(data).then(attr=>{
					let [err,res] = attr
					if(res.data.status){
						// 修改vuex的state,持久化存储
						this.$store.commit('initUserInfo',res.data.data)
						uni.navigateBack({
							delta:1
						})
					}else{
						uni.showToast({
							title: '修改失败',
							icon: 'none'
						});
					}
				})
				
			}
		},
		computed:{
			disabled(){
				return  this.newpassword==="" || this.renewpassword===""
			}
		},
	}
</script>

<style>

</style>
